//
//  QChargeBookCell.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/15.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QChargeBookCell.h"

@interface QChargeBookCell()

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *weakLabel;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameConstr;//标题到cell的顶距离

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;

@property (weak, nonatomic) IBOutlet UIView *titleView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleViewTopConstr;//左边线到顶的距离

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleViewBottomConstr;//左边线到下的距离

@property (weak, nonatomic) IBOutlet UILabel *sMoneyLabel;

@property (weak, nonatomic) IBOutlet UILabel *zMoneyLabel;

@property (weak, nonatomic) IBOutlet UILabel *textNewLabel;//描述

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *proBottomConstr;

@property (weak, nonatomic) IBOutlet UIImageView *dateBgImageView;//水滴

@property (weak, nonatomic) IBOutlet UIImageView *iconImage;

@end

@implementation QChargeBookCell{

   // NSMutableDictionary *dayDic;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.nameLabel.textColor = [QColorUtility defaultBackColor];
}

- (void)setIndexPath:(NSIndexPath *)indexPath{

    _indexPath = indexPath;
}

- (void)setMonthArr:(NSArray *)monthArr{

    _monthArr = monthArr;
    
    if(_indexPath.row == 0){
        _titleView.hidden = NO;
        _dateLabel.hidden = NO;
        _dateBgImageView.hidden = NO;
        _weakLabel.hidden = NO;
        _nameConstr.constant = 55;
        _titleViewTopConstr.constant = 35;
        _titleViewBottomConstr.constant = 0;
    }else{
        QChargeBookModel *model = _monthArr[_indexPath.row];
        NSString *allnewStr = [NSString stringWithFormat:@"%@%@",model.month,model.day];
        
        QChargeBookModel *afterModel = _monthArr[_indexPath.row-1];
        NSString *alloldStr = [NSString stringWithFormat:@"%@%@",afterModel.month,afterModel.day];
        if([allnewStr isEqualToString:alloldStr]){//说明是同一天
            _titleView.hidden = YES;
            _nameConstr.constant = 20;
            _dateLabel.hidden = YES;
            _dateBgImageView.hidden = YES;
            _weakLabel.hidden = YES;
            _titleViewTopConstr.constant = 0;
            _titleViewBottomConstr.constant = 0;
        }else{
            _titleView.hidden = NO;
            _nameConstr.constant = 55;
            _dateLabel.hidden = NO;
            _dateBgImageView.hidden = NO;
            _weakLabel.hidden = NO;
            _titleViewTopConstr.constant = 35;
            _titleViewBottomConstr.constant = 0;
        }
    }
    _proBottomConstr.constant = 20;
}

- (void)setModel:(QChargeBookModel *)model{
    _model = model;
    
    _dateLabel.text = _model.day;
    _weakLabel.text = _model.week;
    _nameLabel.text = _model.accountUseTypeBean.useTypeName;
    if([_nameLabel.text containsString:@"工程费用"]){
        self.iconImage.image = [UIImage imageNamed:@"gcCharge_image"];//需要判断
    }else if ([_nameLabel.text containsString:@"业务费用"]){
        self.iconImage.image = [UIImage imageNamed:@"ywCharge_image"];
    }else if ([_nameLabel.text containsString:@"个人费用"]){
        self.iconImage.image = [UIImage imageNamed:@"mySelfCharge_image"];
    }else if ([_nameLabel.text containsString:@"工程款"]){
        self.iconImage.image = [UIImage imageNamed:@"moneyCharge_image"];
    }else if ([_nameLabel.text containsString:@"租金"]){
        self.iconImage.image = [UIImage imageNamed:@"zCharge_image"];
    }else if ([_nameLabel.text containsString:@"赔付款"]){
        self.iconImage.image = [UIImage imageNamed:@"pCharge_image"];
    }else if ([_nameLabel.text containsString:@"分红"]){
        self.iconImage.image = [UIImage imageNamed:@"fCharge_image"];
    }else if ([_nameLabel.text containsString:@"其他"]){
        self.iconImage.image = [UIImage imageNamed:@"other_image"];
    }
    
    _timeLabel.text = _model.time;
    _textNewLabel.text = _model.remark;
    _moneyLabel.text = [NSString stringWithFormat:@"￥%.2f",[_model.accountTypeBean.money doubleValue]];
    if([_model.accountTypeBean.accountTypeId isEqualToString:@"0"]){//支出
        _moneyLabel.textColor = [QColorUtility colorWithHexString:@"fa8c8c"];
    }else if ([_model.accountTypeBean.accountTypeId isEqualToString:@"1"]){//收入
        _moneyLabel.textColor = [QColorUtility colorWithHexString:@"76d4a7"];
    }
    
    NSMutableDictionary *dayDic = [NSMutableDictionary dictionary];
    for (QChargeBookModel *m in _monthArr) {
        NSString *allStr = m.day;
        NSArray *keys =dayDic.allKeys;
        if([keys containsObject:allStr]){//数据里面是否是当天的 如果是，取出同一天的数据加进去，如果不是创建新的数据，这样数据结构------->      一个月里面有多少天，一天里面有多少条数据
            NSMutableArray *oldArr = dayDic[allStr];
            [oldArr addObject:m];
            [dayDic setObject:oldArr forKey:allStr];
        }else{
            NSMutableArray *dataArr = [NSMutableArray array];
            [dataArr addObject:m];
            [dayDic setObject:dataArr forKey:allStr];
        }
    }
    
    NSString *indexStr = _model.day;
    NSArray *dayArr = dayDic[indexStr];//取出天数据
    double allz = 0.00;//支出
    double alls = 0.00;//收入
    for (QChargeBookModel *dayM in dayArr) {
        if([dayM.accountTypeBean.accountTypeId isEqualToString:@"0"]){//支出
            NSString *moneS = dayM.accountTypeBean.money;
            double z = moneS.doubleValue;
            allz+=z;
            
        }else if ([dayM.accountTypeBean.accountTypeId isEqualToString:@"1"]){//收入
            NSString *moneS = dayM.accountTypeBean.money;
            double s = moneS.doubleValue;
            alls+=s;
        }
    }
    _zMoneyLabel.text = [NSString stringWithFormat:@"支:￥%.2f",allz];
    _sMoneyLabel.text = [NSString stringWithFormat:@"收:￥%.2f",alls];
}

@end
